﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace MINERP.DAL.CustomerManage
{
    public class AutoStartOrderDAL
    {
        //参数
        newSampleYPDBEntities entity = new newSampleYPDBEntities();
        //string conStr = ConfigurationManager.ConnectionStrings["fffff"].ToString();
        string conStr = DALBase.ConnStr;
        SqlConnection con = null;
        /// <summary>
        /// 指定状态的所有订单
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public DataTable GetOrders(int status)
        {
            try
            {
                string sql = "select BagCode,BagName,ShopName,OrderTime,FetchTime,tb_orderbag.Priority,OrderBagId,tb_orderbag.Remark from tb_orderbag,shop where tb_orderbag.shopid=shop.shopid and tb_orderbag.status=" + status;
                DataTable dt = new DataTable();
                using (con = new SqlConnection())
                {
                    SqlCommand cmd = new SqlCommand(sql, con);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    con.Open();
                    adapter.Fill(dt);
                }
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 审核通过
        /// </summary>
        public void CheckAcross(List<int> orderIds, int orderStatus, int infoStatus, int photoStatus, int oldOrderStatus, int oldInfoStatus, int oldPhotoStatus)
        {
            try
            {
                con = new SqlConnection(conStr);
                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                string sqlPhoto = null;
                string sqlInfo = null;
                string sqlOrder = null;
                foreach (int orderId in orderIds)
                {
                    //照片状态
                    sqlPhoto = "update photoinfo set status=" + photoStatus + " where orderproductid in(select orderproductid from orderinfo where orderbagid=" + orderId + ") and status=" + oldPhotoStatus;
                    cmd.CommandText = sqlPhoto;
                    cmd.ExecuteNonQuery();
                    //产品状态
                    sqlInfo = "update orderinfo set status=" + infoStatus + " where orderbagid=" + orderId + " and status=" + oldInfoStatus;
                    cmd.CommandText = sqlInfo;
                    cmd.ExecuteNonQuery();
                    //订单状态
                    sqlOrder = "update tb_orderbag set status=" + orderStatus + " where orderbagid=" + orderId + " and status=" + oldOrderStatus;
                    cmd.CommandText = sqlOrder;
                    cmd.ExecuteNonQuery();
                }
                con.Close();
            }
            catch (Exception ex)
            {
                con.Close();
                throw ex;
            }
        }

        /// <summary>
        /// 通过Id获取订单对象
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public Tb_OrderBag GetOrderById(int orderId)
        {
            try
            {
                Tb_OrderBag order = entity.Tb_OrderBag.Where(p => p.OrderBagId == orderId).First();
                return order;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// entity保存修改
        /// </summary>
        public void Save()
        {
            try
            {
                entity.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
